package pers.cz.orm.db;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import org.junit.Test;
import pers.cz.orm.dao.CommonDbDaoImpl;
import pers.cz.orm.entity.ImsJsonSoap10;
import utils.TimeLag;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: Jef-orm
 * @description: 测试查询
 * @author: Cheng Zhi
 * @create: 2023-09-02 15:07
 **/
public class Select {

    /**
     * @author Chengzhi
     * @date 2023-09-02
     * @测试目的：
     * @预期结果:
     */
    @Test
    public void testSingleQuery(){

        ImsJsonSoap10 imsJsonSoap10 = new ImsJsonSoap10();
        //imsJsonSoap10.getQuery().addCondition(ImsJsonSoap10.Field.interfaceName, "do_query");
        //imsJsonSoap10.getQuery().addCondition(ImsJsonSoap10.Field.jsonId, "ttt");
        imsJsonSoap10.getQuery().addCondition(ImsJsonSoap10.Field.busiCode, 7760);
        List<ImsJsonSoap10> list = new CommonDbDaoImpl().find(imsJsonSoap10);
        System.out.println(list.get(0).getSoDate());
    }

    /**
     * @author Chengzhi
     * @date 2023-09-18
     * @测试目的： 测试ReuseSqlSingle执行效率
     * @预期结果: cost 25787 milliseconds (25 seconds).
     */
    @Test
    public void testReuseSqlSingle(){
        TimeLag timeLag = new TimeLag();
        for (int i=0; i<100; i++) {
            String sql = "select * from aid2.ims_json_soap_1_9 where busi_code = '7759'";
            List<Map> list = new CommonDbDaoImpl().selectAs(sql, Map.class);
        }
        System.out.println(timeLag.cost());
    }

    /**
     * @author Chengzhi
     * @date 2023-09-18
     * @测试目的：测试DefaulteSqlSingle执行效率
     * @预期结果: cost 23062 milliseconds (23 seconds).
     */
    @Test
    public void testDefalutSqlSingle(){
        TimeLag timeLag = new TimeLag();
        for (int i=0; i<1; i++) {
            String sql = "select * from aid2.ims_json_soap_1_9 where busi_code = ?";
            Map<Integer, Object> map = new HashMap<>();
            map.put(1, "7759");
            //map.put(2, "9759");
            List<Map> list = new CommonDbDaoImpl().selectAs(sql, Map.class, map);
            System.out.println(JSON.toJSONString(list));
        }
        //System.out.println(timeLag.cost());
    }

    /**
     * @author Chengzhi
     * @date 2023-09-25
     * @测试目的：执行探测
     * @预期结果:
     */
    @Test
    public void testDetection(){
        for (int i=0; i<10; i++) {
            String sql = "select 1 from dual";
            List list = new CommonDbDaoImpl().selectAs(sql, Map.class);
            System.out.println(JSON.toJSONString(list));
        }
    }

    /**
     * @author Chengzhi
     * @date 2023-10-25
     * @测试目的： 测试SimpleDataSource查询
     * @预期结果:
     */
    @Test
    public void testSimpleDataSource(){

        String sql = "select 1 from dual";
        String url = "jdbc:mysql://10.10.152.228:3306/postgirl";
        String userName = "root";
        String passWord = "a123456";
        SimpleDataSource simpleDataSource = new SimpleDataSource(url, userName, passWord);
        List list = new CommonDbDaoImpl().selectAs(sql, Map.class, simpleDataSource);
        System.out.println(list.get(0).toString());
    }

    public static void main(String[] args) {

        for (int i=0; i<5; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String sql = "select 1 from dual";
                    String url = "jdbc:mysql://10.10.152.228:3306/postgirl";
                    String userName = "root";
                    String passWord = "123456";
                    SimpleDataSource simpleDataSource = new SimpleDataSource(url, userName, passWord);
                    List list = new CommonDbDaoImpl().selectAs(sql, Map.class, simpleDataSource);
                    System.out.println(list.get(0).toString());
                }
            }).start();
        }
    }
}
